<template>
    <div class="w-vw-100 text-center">
        <el-button plain size="large" type="success" @click="handleClick">创建房间</el-button>
    </div>
</template>

<script>
import {defineComponent} from 'vue'
import {ElButton, ElMessage} from 'element-plus'
import axios from "axios";

export default defineComponent({
    components:{
        ElButton
    },
    methods: {
        async handleClick(){
            try {
                let config = {
                    headers: {
                        token: this.$common.getSessionStorageValues('tokenData.token'),
                        refreshtoken: this.$common.getSessionStorageValues('tokenData.refreshtoken')
                    }
                };
                // 发送登录请求
                const response = await axios.post('/api/createroom', {}, config);
                // 判断登录结果
                if (response.data.code === 200 && response.status === 200) {
                    // let socket = new WebSocket('ws://192.168.124.5:9988');
                    let socket = new WebSocket('ws://60.205.220.100:8899');
                    socket.onopen = () => {
                        // console.log("连接成功");
                        socket.send(JSON.stringify({
                            mode: 'createRoom',
                            room_id: this.$common.getSessionStorageValues('roomInfo.room_id'),
                        }));
                        socket.close();
                    };
                    socket.onerror = () => {
                        console.log("连接失败");
                    }
                    this.$common.setSessionStorageValues('roomInfo', response.data.data);
                    ElMessage({
                        message: response.data.msg,
                        type: 'success',
                    })
                    this.$router.replace('/gamematch2');
                } else {
                    ElMessage({
                        message: response.data.msg,
                        type: 'warning',
                    })
                }
            } catch (error) {
                // 发生错误
                ElMessage.error('创建房间出错啦')
                console.error(error);
                // 执行其他错误处理操作...
            }
        },
    }
})
</script>

<style scoped lang="scss">

</style>